package com.quectel.core.module.carparking.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.carparking.dao.CarParkingDao;
import com.quectel.core.module.carparking.dto.CarParkingDto;
import com.quectel.core.module.carparking.entity.CarParkingEntity;
import com.quectel.core.module.carparking.service.CarParkingService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author bobo
 * @email bob.yu@quectel.com
 * @date 2022/02/22 09:44
 */
@DubboService
public class CarParkingServiceImpl implements CarParkingService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CarParkingServiceImpl.class);

    private static final String CAR_PARKING_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "CAR_PARKING_CACHE_BY_ID:";

    @Autowired
    private CarParkingDao carParkingDao;
    @Autowired
    private VillageService villageService;

    /**
     * @param tenantId
     * @param name
     * @return
     */
    @Override
    public CarParkingDto selectByName(Long tenantId, String name) {
        CarParkingEntity entity = carParkingDao.selectOne(
                new LambdaQueryWrapper<CarParkingEntity>()
                        .eq(CarParkingEntity::getTenantId, tenantId)
                        .eq(CarParkingEntity::getName, name)
                        .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return CopyUtils.copyObj(entity, CarParkingDto.class);
    }

    /**
     * 通过小区id查询停车场列表
     *
     * @param villageId
     * @return
     */
    @Override
    public List<CarParkingDto> selectByVillageId(Long villageId) {
        List<CarParkingEntity> list = carParkingDao.selectList(new LambdaQueryWrapper<CarParkingEntity>()
                .eq(CarParkingEntity::getVillageId, villageId)
        );
        return CopyUtils.copyList(list, CarParkingDto.class);
    }

    @Override
    public CarParkingDto selectOne(Long villageId, String code) {
        CarParkingEntity entity = carParkingDao.selectOne(
                new LambdaQueryWrapper<CarParkingEntity>()
                        .eq(CarParkingEntity::getVillageId, villageId)
                        .eq(CarParkingEntity::getCode, code)
        );
        return paddingField(CopyUtils.copyObj(entity, CarParkingDto.class));
    }

    @Override
    public CarParkingDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                CAR_PARKING_CACHE_BY_ID + id,
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public CarParkingDto selectById(Long id) {
        CarParkingEntity entity = carParkingDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, CarParkingDto.class));
    }

    @Override
    public List<CarParkingDto> queryList(Map<String, Object> params) {
        List<CarParkingEntity> list = carParkingDao.queryList(params);
        List<CarParkingDto> result = CopyUtils.copyList(list, CarParkingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private CarParkingDto paddingField(CarParkingDto dto) {
        if (dto != null) {
            VillageDto parkDto = villageService.selectCacheById(dto.getVillageId());
            dto.setVillage(parkDto);
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return carParkingDao.queryTotal(params);
    }

    @Override
    public Long save(CarParkingDto dto) {
        CarParkingEntity entity = CopyUtils.copyObj(dto, CarParkingEntity.class);
        entity.setId(Snowflake.nextId());
        carParkingDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(CarParkingDto dto) {
        CarParkingEntity entity = CopyUtils.copyObj(dto, CarParkingEntity.class);
        carParkingDao.updateById(entity);
        CacheKit.invalidRedisCache(CAR_PARKING_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(CarParkingDto dto) {
        CarParkingEntity entity = CopyUtils.copyObj(dto, CarParkingEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, carParkingDao);

        CacheKit.invalidRedisCache(CAR_PARKING_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        carParkingDao.deleteById(id);

        CacheKit.invalidRedisCache(CAR_PARKING_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
